package snowflake

import (
	"time"

	"github.com/bwmarrin/snowflake"
	"go.uber.org/zap"
)

type Snowflake struct {
	Node *snowflake.Node
}

func NewSnowflake(t time.Time, machineID int64) *Snowflake {
	// 机器 ID
	st, err := time.Parse("2006-01-02", t.Format("2006-01-02"))
	if err != nil {
		zap.S().Error("snowflake init error", zap.Error(err))
	}
	snowflake.Epoch = st.UnixNano() / 1000000
	// 根据指定的开始时间和机器ID，生成节点实例
	node, err := snowflake.NewNode(int64(machineID))
	if err != nil {
		zap.S().Error("snowflake init error", zap.Error(err))
	}
	return &Snowflake{
		Node: node,
	}
}
func (s *Snowflake) GetID() int64 {
	return s.Node.Generate().Int64()
}
